/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package uk.org.aspellclark.agenedb.gedcom.model;

import java.util.ArrayList;
import java.util.HashMap;

public class Submission extends GedcomRecord {

	public static String FIELD_NAME_nameOfFamilyFile = "nameOfFamilyFile";
	public static String FIELD_NAME_templeCode = "templeCode";
	public static String FIELD_NAME_ancestorsCount = "ancestorsCount";
	public static String FIELD_NAME_descendantsCount = "descendantsCount";
	public static String FIELD_NAME_ordinanceProcessFlag = "ordinanceProcessFlag";
	public static String FIELD_NAME_recIdNumber = "recIdNumber";

	public static String DB_TABLE_NAME = "submission";

	public Submitter submitter;
	public String nameOfFamilyFile = "";
	public String templeCode = "";
	public String ancestorsCount = "";
	public String descendantsCount = "";
	public String ordinanceProcessFlag = "";
	public String recIdNumber = "";

	public Submission() {
	}

	/**
	 * Constructor used for creating an Animal from a Database Row. 
	 * @param arow
	 */
	public Submission(HashMap<String, String> arow) {
		this.nameOfFamilyFile = arow.get(FIELD_NAME_nameOfFamilyFile);
		this.templeCode = arow.get(FIELD_NAME_templeCode);
		this.ancestorsCount = arow.get(FIELD_NAME_ancestorsCount);
		this.descendantsCount = arow.get(FIELD_NAME_descendantsCount);
		this.ordinanceProcessFlag = arow.get(FIELD_NAME_ordinanceProcessFlag);
		this.recIdNumber = arow.get(FIELD_NAME_recIdNumber);
	}

	@Override
	public HashMap<Integer, ArrayList<String>> getFields() {
		HashMap<Integer, ArrayList<String>> fields = new HashMap<Integer, ArrayList<String>>();

		int x = 1;
		fields.put(x++, this.getArrayList(FIELD_NAME_UNIQUE_IDENTIFIER, "INTEGER PRIMARY KEY"));
		fields.put(x++, this.getArrayList(FIELD_NAME_nameOfFamilyFile, "VARCHAR(155)"));
		fields.put(x++, this.getArrayList(FIELD_NAME_templeCode, "VARCHAR(155)"));
		fields.put(x++, this.getArrayList(FIELD_NAME_ancestorsCount, "VARCHAR(155)"));
		fields.put(x++, this.getArrayList(FIELD_NAME_descendantsCount, "VARCHAR(155)"));
		fields.put(x++, this.getArrayList(FIELD_NAME_ordinanceProcessFlag, "VARCHAR(155)"));
		fields.put(x++, this.getArrayList(FIELD_NAME_recIdNumber, "VARCHAR(155)"));

		return fields;
	}

	/**
	 * @param xref
	 * @return
	 */
	public String getSelectByXRefSql(String xref) {
		return ("select * from " + this.getTableName() + " where " + FIELD_NAME_GEDCOM_XREF + "=" + xref);

	}

	@Override
	public String getSqlInsert() {
		StringBuilder sqlStmt = new StringBuilder(super.getSqlInsert());
		sqlStmt.append("\"").append(this.nameOfFamilyFile).append("\"");
		sqlStmt.append(",\"").append(this.templeCode).append("\"");
		sqlStmt.append(",\"").append(this.ancestorsCount).append("\"");
		sqlStmt.append(",\"").append(this.descendantsCount).append("\"");
		sqlStmt.append(",\"").append(this.ordinanceProcessFlag).append("\"");
		sqlStmt.append(",\"").append(this.recIdNumber).append("\"");

		sqlStmt.append(");");
		return sqlStmt.toString();
	}

	/**
	 * return the sql code to update a computer in the database.
	 */
	@Override
	public String getSqlUpdate() {
		StringBuilder sqlStmt = new StringBuilder(super.getSqlUpdate());
		sqlStmt.append(" ").append(FIELD_NAME_nameOfFamilyFile).append("=\"").append(this.nameOfFamilyFile).append(
				"\"");
		sqlStmt.append(",").append(FIELD_NAME_templeCode).append("=\"").append(this.templeCode).append("\"");
		sqlStmt.append(",").append(FIELD_NAME_ancestorsCount).append("=\"").append(this.ancestorsCount).append("\"");
		sqlStmt.append(",").append(FIELD_NAME_descendantsCount).append("=\"").append(this.descendantsCount).append(
				"\"");
		sqlStmt.append(",").append(FIELD_NAME_ordinanceProcessFlag).append("=\"").append(this.ordinanceProcessFlag)
				.append("\"");
		sqlStmt.append(",").append(FIELD_NAME_recIdNumber).append("=\"").append(this.recIdNumber).append("\"");

		sqlStmt.append(" where ").append(FIELD_NAME_UNIQUE_IDENTIFIER).append("=").append(this.uniqueIdentifier);
		return sqlStmt.toString();
	}

	@Override
	public String getTableName() {
		return DB_TABLE_NAME;
	}

}
